/*
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'avocaod systems, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 *
 *	@project	BrowserChooser 0.2
 *	@package	de.fuhrmeister.browserchooser.view
 *	@file		OptionsView.java
 *
 *	@date		01.10.2008
 *	@author		Marcus Fuhrmeister
 */
package de.fuhrmeister.browserchooser.view;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.util.EventListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeListener;

/**
 * Options view class used in the settings dialog.
 * 
 *	@date 20.11.2008
 *	@author		Marcus Fuhrmeister
 */
public class SettingsView extends JDialog implements View {

	private static final long serialVersionUID = -8256621839595100301L;
	private final JTabbedPane tabbedPane = new JTabbedPane();
	private final JPanel buttonPanel = new JPanel();
	private final JButton buttonSave = new JButton();
	private final JButton buttonAbort = new JButton();

	public static final int ITEM_BUTTON_SAVE = 1;
	public static final int ITEM_BUTTON_ABORT = 2;

	/**
	 * Constructor to create a new view.
	 * 
	 * @param parent the parent frame
	 * @param title the title of the view
	 * @param image the image of the view
	 */
	public SettingsView(Frame parent, String title, Image image) {
		super(parent);
		this.setTitle(title);
		this.setIconImage(image);
	}

	/**
	 * Inits the view components.
	 */
	public void initItems() {
		this.setLayout(new BorderLayout());
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		this.add(tabbedPane, BorderLayout.CENTER);
		buttonPanel.setLayout(null);
		buttonPanel.add(buttonSave);
		buttonPanel.add(buttonAbort);
		this.add(buttonPanel, BorderLayout.SOUTH);
	}

	/**
	 * Adds a tab to the view.
	 * 
	 * @param title the title of the new tab
	 * @param panel the panel to add
	 */
	public void addView(final String title, final JComponent panel) {
		tabbedPane.addTab(title, panel);
	}
	
	/**
	 * Inserts a tab into the view at <i>index</i>.
	 * 
	 * @param title the title of the new tab
	 * @param panel the panel to add
	 * @param index the index where to insert the tab
	 */
	public void insertView(final String title, final JComponent panel, final int index) {
		tabbedPane.insertTab(title, null, panel, null, index);
	}

	/**
	 * Resets a tab by removing it and inserting the new one.
	 * 
	 * @param title the title of the new tab
	 * @param panel the panel to add
	 * @param index the index where to insert the tab
	 */
	public void resetView(final String title, final JComponent panel, final int index) {
		tabbedPane.removeTabAt(index);
		tabbedPane.insertTab(title, null, panel, null, index);
	}
	
	/**
	 * Removes the tab at <i>index</i>.
	 * 
	 * @param index the index of the tab to remove
	 */
	public void deleteView(final int index) {
		tabbedPane.removeTabAt(index);
	}

	/**
	 * Returns the component at <i>index</i>.
	 * 
	 * @param index the index of the tab to return
	 * @return the component at <i>index</i>
	 */
	public Component getView(final int index) {
		return tabbedPane.getComponentAt(index);
	}
	
	/**
	 * Returns the tab count.
	 * 
	 * @return the tab count
	 */
	public int getViewCount() {
		return tabbedPane.getTabCount();
	}

	/**
	 * Sets the button text value.
	 * 
	 * @param item the key of the item to set
	 * @param text the value of the button
	 */
	public void setButtonText(final int item, final String text) {
		switch (item) {
		case SettingsView.ITEM_BUTTON_SAVE:
			buttonSave.setText(text);
		case SettingsView.ITEM_BUTTON_ABORT:
			buttonAbort.setText(text);
		}
	}

	/**
	 * Sets the bounds of the button.
	 * 
	 * @param item the key of the item to set
	 * @param x the x location
	 * @param y the y location
	 * @param width the width of the button
	 * @param height the height of the button
	 */
	public void setButtonBounds(final int item, final int x, final int y, final int width, final int height) {
		switch (item) {
		case SettingsView.ITEM_BUTTON_SAVE:
			buttonSave.setBounds(x, y, width, height);
		case SettingsView.ITEM_BUTTON_ABORT:
			buttonAbort.setBounds(x, y, width, height);
		}
	}

	/**
	 * Sets the action command to the button.
	 * 
	 * @param item the key of the item to set
	 * @param text the action command string
	 */
	public void setActionCommand(final int item, final String text) {
		switch (item) {
		case SettingsView.ITEM_BUTTON_SAVE:
			buttonSave.setActionCommand(text);
		case SettingsView.ITEM_BUTTON_ABORT:
			buttonAbort.setActionCommand(text);
		}
	}

	/**
	 * Sets the button panel size.
	 * 
	 * @param width the width of the button panel
	 * @param height the height of the button panel
	 */
	public void setButtonPanelSize(final int width, final int height) {
		buttonPanel.setPreferredSize(new Dimension(width, height));
		buttonPanel.setSize(width, height);
	}

	/**
	 * Focuses on the tab with the given index.
	 * 
	 * @param index the index of the tab to focus on 
	 */
	public void focusView(final int index) {
		tabbedPane.setSelectedIndex(index);
	}

	/**
	 * Checks if the tab at <i>index</i> is enabled.
	 * 
	 * @param index the index of the tab to check
	 * @return true if the tab at <i>index</i> is enabled
	 */
	public boolean isViewEnabled(int index) {
		return tabbedPane.isEnabledAt(index);
	}
	
	/**
	 * Enables/Disables the tab at <i>index</i>.
	 * 
	 * @param index the index of the tab to enable/disable
	 * @param enabled true if the tab shall be enabled
	 */
	public void setViewEnabled(final int index, final boolean enabled) {
		tabbedPane.setEnabledAt(index, enabled);
	}

	/**
	 * Registers the listeners.
	 */
	public void registerListener(final EventListener el) {
		if (el instanceof ActionListener) {
			buttonSave.addActionListener((ActionListener) el);
			buttonAbort.addActionListener((ActionListener) el);
		}
		if (el instanceof ChangeListener) {
			tabbedPane.addChangeListener((ChangeListener) el);
		}
		if (el instanceof WindowListener) {
			this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
			this.addWindowListener((WindowListener) el);
		}

	}
}
